//
//  YTKBaseRequest+NetErrorToast.m
//  Locbest
//
//  Created by 文龙 张 on 16/1/11.
//  Copyright © 2016年 ios.wenlong. All rights reserved.
//

#import "YTKBaseRequest+Common.h"
#import <objc/runtime.h>
#import "Utils.h"
#import "AppDelegate.h"

@implementation YTKBaseRequest (Common)

+ (void)load {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        swizzleMethod([self class], @selector(requestFailedFilter), @selector(swizzled_requestFailedFilter));
        swizzleMethod([self class], @selector(requestCompleteFilter), @selector(swizzled_requestCompleteFilter));
        swizzleMethod([self class], @selector(requestHeaderFieldValueDictionary), @selector(swizzled_requestHeaderFieldValueDictionary));
    });
}

void swizzleMethod(Class class, SEL originalSelector, SEL swizzledSelector)
{
    // the method might not exist in the class, but in its superclass
    Method originalMethod = class_getInstanceMethod(class, originalSelector);
    Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector);
    
    // class_addMethod will fail if original method already exists
    BOOL didAddMethod = class_addMethod(class, originalSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod));
    
    // the method doesn’t exist and we just added one
    if (didAddMethod) {
        class_replaceMethod(class, swizzledSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod));
    }
    else {
        method_exchangeImplementations(originalMethod, swizzledMethod);
    }
}

#pragma mark - Method Swizzling

- (void)swizzled_requestFailedFilter
{
    [self swizzled_requestFailedFilter];
    
    if (self.error.code == -1009)
    {
//        [PromptView autoHideWithText:@"请检查网络是否连接！"];
    }
    else if (self.error.code == -1001)
    {
//        [PromptView autoHideWithText:@"请求超时，请稍后重试！"];
    }
    else
    {
        NSLog(@"error====%@",self.error.description);
//        [PromptView autoHideWithText:@"服务器异常，请稍后重试！"];
    }
    
}

- (NSDictionary *)swizzled_requestHeaderFieldValueDictionary
{
    [self swizzled_requestHeaderFieldValueDictionary];
    NSString *token = [[NSUserDefaults standardUserDefaults] valueForKey:HTTPTOKEN];
    if (token.length > 0) {
        return @{@"authorization":token};
    } else {
        return @{};
    }
}

- (void)swizzled_requestCompleteFilter {
    [self swizzled_requestCompleteFilter];
    
//    NSDictionary *response = self.responseObject;
//    if (response && [response isKindOfClass:[NSDictionary class]]) {
//        NSNumber *resultCode = [response objectForKey:REQUEST_RESCODE];
//        if (resultCode.integerValue == 999) {
//            [[NSUserDefaults standardUserDefaults] removeObjectForKey:HTTPTOKEN];
//            [[NSUserDefaults standardUserDefaults] synchronize];
//            //token超时
//            UITabBarController *tabbarController = (UITabBarController *)TheAppDelegate.window.rootViewController;
//            UINavigationController *navigationCtl = tabbarController.selectedViewController;
//            UIViewController *vc = [LoginStory instantiateInitialViewController];
//            [navigationCtl presentViewController:vc animated:YES completion:nil];
//        }
//    }
}
@end
